package com.lexton.toolbox.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;

import com.lexton.toolbox.parser.SupplierExcelParser;
import com.lexton.util.excel.ExcelReadUtils;

public class ParserWindow extends JFrame {
	private JTextField excelPath = new JTextField(10);
	private JTextField exportPath = new JTextField(10);
	private JPanel center = new JPanel();
	private static final long serialVersionUID = 6179537869196143696L;
	private JFrame frame = null;
	public ParserWindow() {
		frame = this;
		frame.setIconImage(new ImageIcon(this.getClass().getResource("/icon/logo/logo128.png")).getImage());
		this.setLocationRelativeTo(null);
		this.setSize(1000, 600);
		this.setMinimumSize(new Dimension(1000, 600));
		this.setTitle("供应商产品EXCEL表格分析");
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.add(new JMenu("文件"));
		this.setJMenuBar(menuBar);
		
		GridLayout northLayout = new GridLayout(4, 1);
		northLayout.setVgap(5);
		JPanel north = new JPanel();
		north.setLayout(northLayout);
		north.setBorder(BorderFactory.createEmptyBorder(20,20,10,20));//设置内边距
		
		JPanel centerRow1 = new JPanel();
		FlowLayout flow = new FlowLayout(FlowLayout.LEADING);
		flow.setHgap(0);flow.setVgap(0);
		centerRow1.setLayout(flow);
		centerRow1.add(new JLabel("内容排版样式: "));
		JRadioButton radio1 = new JRadioButton("纵向表雷克斯登");
		JRadioButton radio2 = new JRadioButton("横向表法罗兰");
		JRadioButton radio3 = new JRadioButton("其他东南亚风情");
		ButtonGroup bg = new ButtonGroup();
		radio1.setSelected(true);
		bg.add(radio1);
		bg.add(radio2);
		bg.add(radio3);
		radio1.setSelected(true);
		
		centerRow1.add(radio1);
		centerRow1.add(radio2);
		centerRow1.add(radio3);
		north.add(centerRow1);
		
		JPanel centerRow2 = new JPanel();
		FlowLayout flow2 = new FlowLayout(FlowLayout.LEADING);
		flow2.setHgap(0);flow2.setVgap(0);
		centerRow2.setLayout(flow2);
		centerRow2.add(new JLabel("表头编辑情况: "));
		List<JRadioButton> headerModeList = new ArrayList<JRadioButton>();
		JRadioButton radio4 = new JRadioButton("只有第一个工作表设置了映射");
		JRadioButton radio5 = new JRadioButton("每个工作的首行都设置了映射");
		radio4.setSelected(true);
		headerModeList.add(radio4);
		headerModeList.add(radio5);
		
		ButtonGroup bg2 = new ButtonGroup();
		bg2.add(radio4);
		bg2.add(radio5);
		radio1.setSelected(true);
		
		centerRow2.add(radio4);
		centerRow2.add(radio5);
		north.add(centerRow2);
		
		GridLayout gridRowLayout = new GridLayout(1, 3);
		gridRowLayout.setHgap(15);
		
		JPanel centerRow3 = new JPanel();
		centerRow3.setLayout(gridRowLayout);
		centerRow3.setBounds(0, 0, 0, 0);
		centerRow3.add(new JLabel("供应商产品表格："));
		excelPath.setEnabled(false);
		centerRow3.add(excelPath);
		JButton excelBtn = new JButton("选择EXCEL文件");
		centerRow3.add(excelBtn);
		north.add(centerRow3);
		
		JPanel centerRow4 = new JPanel();
		centerRow4.setLayout(gridRowLayout);
		//centerRow4.setBounds(0, 0, 0, 0);
		centerRow4.add(new JLabel("处理结果保存目录："));
		exportPath.setEnabled(false);
		centerRow4.add(exportPath);
		JButton exportBtn = new JButton("选择保存目录");
		centerRow4.add(exportBtn);
		north.add(centerRow4);
		
		this.add(north, BorderLayout.NORTH);
		
		this.add(center, BorderLayout.CENTER);
		
		JPanel south = new JPanel();
		south.setLayout(new GridLayout(1, 2));
		south.setBorder(BorderFactory.createEmptyBorder(20,20,10,20));
		JButton startParseBtn = new JButton("开始转换");
		startParseBtn.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.blue));
		startParseBtn.setForeground(Color.white);
		JCheckBox saveImage = new JCheckBox("将EXCEL中的图片保存到目录");
		saveImage.setForeground(Color.RED);
		south.add(saveImage);
		south.add(startParseBtn);
		this.add(south, BorderLayout.SOUTH);
		
		List<JCheckBox> checkBoxList = new ArrayList<JCheckBox>();
		excelBtn.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				if (file != null && file.isFile()) {
					center.removeAll();
					checkBoxList.clear();
					excelPath.setText(file.getAbsolutePath());
					final Map<Integer, String> sheetInfo = ExcelReadUtils.readSheetInfo(excelPath.getText());
					center.setLayout(new FlowLayout());
					center.add(new JLabel("选择需要导入的EXCEL的工作表："));
					sheetInfo.forEach((i, name)->{
						JCheckBox checkBox = new JCheckBox(name);
						center.add(checkBox);
						checkBoxList.add(checkBox);
					});
					
					frame.pack();
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});
		
		exportBtn.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFileChooser jfc = new JFileChooser();
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				jfc.showDialog(new JLabel(), "选择");
				File file = jfc.getSelectedFile();
				if (file != null && file.isDirectory()) {
					exportPath.setText(file.getAbsolutePath()+File.separator);
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});
		
		startParseBtn.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				startParseBtn.setEnabled(false);
				if (null == excelPath.getText() || "".equals(excelPath.getText())) {
					JOptionPane.showMessageDialog(null, "请选择供应商的产品目录表格");
					startParseBtn.setEnabled(true);
					return;
				}
				
				if (null == exportPath.getText() || "".equals(exportPath.getText())) {
					JOptionPane.showMessageDialog(null, "请选择导出目录");
					startParseBtn.setEnabled(true);
					return;
				}
				
				Map<Integer, String> pickedSheets = new HashMap<Integer, String>();
				for (int i=0; i<checkBoxList.size(); i++) {
					JCheckBox cb = checkBoxList.get(i);
					if (cb.isSelected()) {
						pickedSheets.put(i, cb.getText());
					}
				}
				
				if (pickedSheets.size() == 0) {
					JOptionPane.showMessageDialog(null, "请选择需要解析的EXCEL工作表");
					startParseBtn.setEnabled(true);
					return;
				}
				
				try {
					String layoutMode = null;
					if (radio1.isSelected()) {
						layoutMode = "STYLE01";
					} else if (radio2.isSelected()) {
						layoutMode = "STYLE02";
					} else if (radio3.isSelected()) {
						layoutMode = "STYLE03";
					}
					String headerMode = null;
					if (radio4.isSelected()) {
						headerMode = "FIRST_SHEET";
					} else if (radio5.isSelected()) {
						headerMode = "EVERY_SHEET";
					}
					
					SupplierExcelParser.parseToFile(excelPath.getText(), pickedSheets, headerMode, layoutMode, exportPath.getText(), saveImage.isSelected());

				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, e.getMessage());
					return;
				} finally {
					startParseBtn.setEnabled(true);
				}
				JOptionPane.showMessageDialog(null, "处理完成!");
			}

			public void mouseEntered(MouseEvent arg0) {}

			public void mouseExited(MouseEvent arg0) {}

			public void mousePressed(MouseEvent arg0) {}

			public void mouseReleased(MouseEvent arg0) {}
		});
	}

}
